/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.nifi.processors.huawei.obs;

import com.obs.services.model.StorageClassEnum;
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.apache.nifi.processors.huawei.obs.FetchOBSObject.RANGE_LENGTH;
import static org.apache.nifi.processors.huawei.obs.FetchOBSObject.RANGE_START;
import static org.junit.jupiter.api.Assertions.assertEquals;


/**
 * Provides integration level testing with actual HuaweiYun OBS resources for {@link FetchOBSObject} and requires additional configuration and resources to work.
 */
public class ITFetchOBSObject extends AbstractOBSIT {
    @Test
    public void testSimpleGet() throws IOException, InterruptedException {
        putTestFile("test-file");
        final TestRunner runner = initRunner(new FetchOBSObject());
        final Map<String, String> attrs = new HashMap<>();
        attrs.put("filename", "test-file");
        runner.enqueue(new byte[0], attrs);

        runner.run(1);

        runner.assertAllFlowFilesTransferred(FetchOBSObject.REL_SUCCESS, 1);
        final List<MockFlowFile> ffs = runner.getFlowFilesForRelationship(FetchOBSObject.REL_SUCCESS);
        MockFlowFile ff = ffs.get(0);
        ff.assertContentEquals(getFileFromResourceName(SAMPLE_FILE_RESOURCE_NAME));
    }

    @Test
    public void testSimpleGetEncrypted() throws IOException {
        putTestFileEncrypted("test-file", getFileFromResourceName(SAMPLE_FILE_RESOURCE_NAME));
        final TestRunner runner = initRunner(new FetchOBSObject());
        final Map<String, String> attrs = new HashMap<>();
        attrs.put("filename", "test-file");
        runner.enqueue(new byte[0], attrs);

        runner.run(1);

        runner.assertAllFlowFilesTransferred(FetchOBSObject.REL_SUCCESS, 1);
        final List<MockFlowFile> ffs = runner.getFlowFilesForRelationship(FetchOBSObject.REL_SUCCESS);
        MockFlowFile ff = ffs.get(0);
        ff.assertContentEquals(getFileFromResourceName(SAMPLE_FILE_RESOURCE_NAME));
    }

    @Test
    public void testTryToFetchNotExistingFile() throws IOException {
        final TestRunner runner = initRunner(new FetchOBSObject());
        final Map<String, String> attrs = new HashMap<>();
        attrs.put("filename", "no-such-a-file");
        runner.enqueue(new byte[0], attrs);
        runner.run(1);

        runner.assertAllFlowFilesTransferred(FetchOBSObject.REL_FAILURE, 1);
    }

    @Test
    public void testContentsOfFileRetrieved() throws IOException, InterruptedException {
        String key = "folder/1.txt";
        putTestFile(key);
        final TestRunner runner = initRunner(new FetchOBSObject());
        final Map<String, String> attrs = new HashMap<>();
        attrs.put("filename", key);
        runner.enqueue(new byte[0], attrs);
        runner.run(1);

        runner.assertAllFlowFilesTransferred(FetchOBSObject.REL_SUCCESS, 1);
        final List<MockFlowFile> ffs = runner.getFlowFilesForRelationship(FetchOBSObject.REL_SUCCESS);
        final MockFlowFile out = ffs.iterator().next();
        final byte[] expectedBytes = Files.readAllBytes(getResourcePath(SAMPLE_FILE_RESOURCE_NAME));
        out.assertContentEquals(new String(expectedBytes));
        for (final Map.Entry<String, String> entry : out.getAttributes().entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }

    @Test
    public void testFetchRangeOfFile() {
        TestRunner putRunner = initRunner(new PutOBSObject());
        putRunner.setProperty(PutOBSObject.MULTIPART_THRESHOLD, "50 MB");
        putRunner.setProperty(PutOBSObject.MULTIPART_PART_SIZE, "50 MB");
        putRunner.setProperty(PutOBSObject.STORAGE_CLASS, StorageClassEnum.STANDARD.name());
        final Map<String, String> putAttrs = new HashMap<>();
        String fileName = "testStorageClasses/large_" + StorageClassEnum.STANDARD + ".dat";
        putAttrs.put("filename", fileName);
        putRunner.enqueue(new byte[50 * 1024 * 1024 + 1], putAttrs);
        putRunner.run();
        putRunner.assertAllFlowFilesTransferred(PutOBSObject.REL_SUCCESS, 1);
        final TestRunner runner = initRunner(new FetchOBSObject());
        final Map<String, String> attrs = new HashMap<>();
        attrs.put("filename", fileName);
        runner.setProperty(RANGE_START,"1 MB");
        runner.setProperty(RANGE_LENGTH,"20 KB");
        runner.enqueue(new byte[0], attrs);
        runner.run(1);

        runner.assertAllFlowFilesTransferred(FetchOBSObject.REL_SUCCESS, 1);
        final List<MockFlowFile> ffs = runner.getFlowFilesForRelationship(FetchOBSObject.REL_SUCCESS);
        MockFlowFile ff = ffs.get(0);
        long length = ff.getContent().length();
        assertEquals(length, 20480);
    }

}
